package com.jt.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author 金帮嘉诚 @date 2021-08-24 星期二 19:28
 */
@Configuration
public class CorsFilterConfig {
    @Bean
    public CorsWebFilter corsWebFilter(){
        //注意要导的包
        //01.构建基于url方式的跨域配置
        UrlBasedCorsConfigurationSource anyName =
                new UrlBasedCorsConfigurationSource();

        //02.进行跨域配置
        CorsConfiguration config = new CorsConfiguration();
        //02.1 允许所有ip:port进行跨域
        config.addAllowedOrigin("*");
        //02.2 允许所有请求头跨域
        config.addAllowedHeader("*");
        //02.3 允许所有请求方式跨域
        config.addAllowedMethod("*");
        //02.4 允许携带有效cookie进行跨域
        config.setAllowCredentials(true);

        //
        anyName.registerCorsConfiguration("/**",config);

        //第一步写这个,然后根据所需要的参数类型,构建所需参数类型的对象
        return new CorsWebFilter(anyName);
    }
}
